package com.vkmessenger.gui.activities.contacts;

import android.app.Activity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import com.android.internal.util.Predicate;
import com.vkmessenger.R;
import com.vkmessenger.core.localcache.FriendsCache;
import com.vkmessenger.core.localcache.Notifiable;
import com.vkmessenger.net.json.FriendsResponse;

import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Evgeniy Krasko
 * Date: 21.06.12
 * Time: 19:34
 */
public abstract class CommonContactsActivity extends Activity implements Notifiable {
    private static Logger logger = Logger.getLogger(CommonContactsActivity.class.getName());

    protected EditText search;
    protected MyScrollView scrollView;
    protected ImageView fakeImageView;

    public FriendsResponse.FriendResponse[] bestFiltered = null, allFiltered = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_all);

        scrollView = ((MyScrollView) findViewById(R.id.contacts_scroll));
        search = (EditText) findViewById(R.id.search_in_contacts);
        fakeImageView = (ImageView) findViewById(R.id.fakeImageView);

        search.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            public void afterTextChanged(Editable editable) {
                CommonContactsActivity.this.notify(this);
            }
        });

        scrollView.setParent(this);
        FriendsCache.getInstance().addListener(this);
    }

    private FriendsResponse.FriendResponse[] filter(FriendsResponse.FriendResponse[] all) {
        Predicate<FriendsResponse.FriendResponse> pred = getFilter();

        int cnt = 0;
        for (FriendsResponse.FriendResponse friend : all) {
            if (pred.apply(friend)) ++cnt;
        }

        FriendsResponse.FriendResponse[] online = new FriendsResponse.FriendResponse[cnt];
        cnt = 0;
        for (FriendsResponse.FriendResponse friend : all) {
            if (pred.apply(friend)) online[cnt++] = friend;
        }
        return online;
    }


    public void drawFriends(MyScrollView view, Canvas canvas) {
        if (bestFiltered == null || allFiltered == null) {
            logger.info("Required contact data recalc: " + this);

            setDataForDrawing();
            int h = drawAndGetHeight(view, canvas);
            fakeImageView.setMinimumHeight(h);
            fakeImageView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.FILL_PARENT, h));
        }
        drawAndGetHeight(view, canvas);
    }

    private int drawAndGetHeight(MyScrollView view, Canvas canvas) {
        int h = 0;
        h = view.drawFriends(canvas, h, bestFiltered, false, drawOnline());
        h = view.drawFriends(canvas, h, allFiltered, true, drawOnline());
        return h;
    }

    protected abstract boolean drawOnline();

    public abstract Predicate<FriendsResponse.FriendResponse> getFilter();

    public static Predicate<FriendsResponse.FriendResponse> getFilterByName(final String name) {
        return new Predicate<FriendsResponse.FriendResponse>() {
            String s = name.toLowerCase();

            public boolean apply(FriendsResponse.FriendResponse friend) {
                return isSubstringIgnoreCase(friend.first_name)
                        || isSubstringIgnoreCase(friend.last_name);
            }

            private boolean isSubstringIgnoreCase(String a) {
                return a != null && a.toLowerCase().contains(s);
            }
        };
    }

    public void notify(Object invoker) {
        bestFiltered = allFiltered = null;
        scrollView.postInvalidate();

        logger.info("Activity " + this + " notified of changes");
    }

    private void setDataForDrawing() {
        final FriendsCache cache = FriendsCache.getInstance();
        synchronized (FriendsCache.getInstance()) {
            bestFiltered = filter(cache.getBestFriends());
            allFiltered = filter(cache.getAllFriends());
        }
    }


}